package com.ddxz.middleground.gateway.service.impl;

import com.ddxz.middleground.gateway.service.GatewayService;
import com.ddxz.tool.core.convert.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;

import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/7/26
 */

@Service
public class GatewayServiceImpl implements GatewayService {
    @Override
    public boolean isPermission(String roleIds, String url) {
        Set<Object> paths = new HashSet<>();
        StringUtil.getList(roleIds).forEach(roleId ->
            paths.addAll(Objects.requireNonNull(redisTemplate.opsForSet().members(roleId)))
        );

        for(Object path : paths) {
            if(pathMatcher.match((String)path, url)) {
                return true;
            }
        }
        return false;
    }

    private AntPathMatcher pathMatcher = new AntPathMatcher();

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;
}
